package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.DialogHelper;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.UserInfoManager;
import com.bnyy.medicalHousekeeper.moudle.message.MessageManager;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.chat.ChatMessage;
import com.bnyy.message.bean.chat.message_data.MessageData;
import com.bnyy.message.bean.chat.message_data.VerificationMessage;
import com.google.gson.Gson;

import java.util.HashMap;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/1
 **/
public class VerificationMessageAdapter extends BaseNormalListAdapter<ChatMessage, VerificationMessageAdapter.ViewHolder> {

    public VerificationMessageAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_verification, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VerificationMessageAdapter.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ChatMessage chatMessage = getData(position);

        GlideHelper.setCircleImage(mContext, chatMessage.getUser_img(), holder.iv, R.mipmap.icon_default_header_man);
        SpanUtils.with(holder.tv)
                .append(chatMessage.getUsername()).setBold()
                .appendLine()
                .append(chatMessage.getContent()).setForegroundColor(ContextCompat.getColor(mContext, R.color.gray_light))
                .create();
//        VerificationMessage verificationMessage = (VerificationMessage) MessageManager.getMessageData(chatMessage.getMessageType(), chatMessage.getMsgId());
        VerificationMessage verificationMessage = (VerificationMessage) chatMessage.getMessageData();
        if (verificationMessage != null) {
            chatMessage.setMessageData(verificationMessage);

            int status = verificationMessage.getStatus();

            GradientDrawable bg = new GradientDrawable();
            bg.setCornerRadius(360);
            switch (status) {
                case 0: // 待审核
                    if (chatMessage.getSenderId() == UserInfoManager.getInstance().getLoginUserId()) {
                        holder.llOperate.setVisibility(View.GONE);
                        holder.tvStatus.setVisibility(View.VISIBLE);
                        holder.tvStatus.setText("等待管理员审核");
                        holder.tvStatus.setTextColor(ContextCompat.getColor(mContext, R.color.green_theme));

                        bg.setColor(Color.parseColor("#EAF8EF"));
                        holder.tvStatus.setBackground(bg);
                    } else {
                        holder.llOperate.setVisibility(View.VISIBLE);
                        holder.tvStatus.setVisibility(View.GONE);
                    }
                    break;
                case 1: // 同意
                    holder.llOperate.setVisibility(View.GONE);
                    holder.tvStatus.setVisibility(View.VISIBLE);
                    holder.tvStatus.setTextColor(ContextCompat.getColor(mContext, R.color.green_theme));
                    holder.tvStatus.setText("已同意");
                    bg.setColor(ContextCompat.getColor(mContext, R.color.gray_background));
                    holder.tvStatus.setBackground(bg);
                    break;
                case 2: // 不同意
                    holder.llOperate.setVisibility(View.GONE);
                    holder.tvStatus.setVisibility(View.VISIBLE);
                    holder.tvStatus.setText("已拒绝");
                    holder.tvStatus.setTextColor(Color.parseColor("#FC7675"));
                    bg.setColor(Color.parseColor("#FDF4F5"));
                    holder.tvStatus.setBackground(bg);
                    break;
            }
        }

        holder.tvRefuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify(position, 2);
            }
        });

        holder.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify(position, 1);
            }
        });
    }

    private void verify(int position, int status) {
        ChatMessage chatMessage = getData(position);
        Log.e("verify", new Gson().toJson(chatMessage));
        MessageData messageData = chatMessage.getMessageData();
        if (messageData != null) {
            if (messageData instanceof VerificationMessage) {
                VerificationMessage verificationMessage = (VerificationMessage) messageData;

                HashMap<String, Object> params = new HashMap<>();
                params.put("enter_id", verificationMessage.getEnter_id());
                params.put("status", status);
                RequestManager requestManager = RequestManager.getInstance();
                requestManager.request(requestManager.mMessageRetrofitService.verify(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<VerificationMessage>() {

                            @Override
                            public void onSuccess(VerificationMessage message) {
                                super.onSuccess(message);

                                verificationMessage.setStatus(message.getStatus());
                                MessageManager.save(verificationMessage);
                                notifyItemChanged(position);
                            }

                            @Override
                            public void showMessage(String msg) {
                                super.showMessage(msg);
                                if (!TextUtils.isEmpty(msg)) {
                                    DialogHelper.showTipsDialog(mContext, msg);
                                }
                            }
                        });
            }
        }
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView iv;
        TextView tv;
        LinearLayout llOperate;
        TextView tvRefuse;
        TextView tvConfirm;
        TextView tvStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
            llOperate = itemView.findViewById(R.id.ll_operate);
            tvRefuse = itemView.findViewById(R.id.tv_refuse);
            tvConfirm = itemView.findViewById(R.id.tv_confirm);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }

}
